package domain.entities;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 26/10/12
 * Time: 15:09
 * To change this template use File | Settings | File Templates.
 */
public class EquipmentTypeDAO extends HibernateDaoSupport {
    private static final Logger log = LoggerFactory
            .getLogger(EquipmentTypeDAO.class);

    
    protected void initDao() {
        // do nothing
    }

    public void save(EquipmentType transientInstance) {
        log.debug("saving EquipmentType instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(EquipmentType persistentInstance) {
        log.debug("deleting EquipmentType instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public EquipmentType findById(java.lang.Integer id) {
        log.debug("getting EquipmentType instance with id: " + id);
        try {
            EquipmentType instance = (EquipmentType) getHibernateTemplate()
                    .get("domain.entities.EquipmentType", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findAll() {
        log.debug("finding all EquipmentType instances");
        try {
            String queryString = "from EquipmentType where active = true order by description";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public EquipmentType merge(EquipmentType detachedInstance) {
        log.debug("merging EquipmentType instance");
        try {
            EquipmentType result = (EquipmentType) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(EquipmentType instance) {
        log.debug("attaching dirty EquipmentType instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(EquipmentType instance) {
        log.debug("attaching clean EquipmentType instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static EquipmentTypeDAO getFromApplicationContext(
            ApplicationContext ctx) {
        return (EquipmentTypeDAO) ctx.getBean("EquipmentTypeDAO");
    }
}
